<?php
namespace Admin\Controller;
use Think\Controller;
header("Content-Type:text/html; charset=utf-8");

class LoginController extends Controller {

    //登录
    public function login()
    {
        $this->display();
    }

    //登录验证
    public function loginYz()
    {

        $username   =   trim(I('post.username'));
        $pwd    =   trim(I('post.password'));

        $user=M('admin')->where(array('name'=>$username))->find();
        if(!$user) die("<script>alert('该账号不存在');history.back(-1);</script>");
        elseif($user['pwd']!= md5($pwd)) die("<script>alert('密码不正确');history.back(-1);</script>");
        else{
            session('user',$user['name']);
            session('user_id',$user['id']);
            session('type',$user['type']);
            $data['login_time'] = date('Y-m-d H:i:s');
            M('users')->where(array('name'=>$username))->save($data);
            $this->redirect('Admin/Index/index');
        }

    }

    public function regDo()
    {
        $data   =   I('post.');

        //手机号格式 / 存在验证
        if(!isMobile($data['name'])){
            die("<script>alert('手机号格式有误');history.back(-1);</script>");
        }
        if(existData('users','name',$data['name'])){
            die("<script>alert('手机号已被注册');history.back(-1);</script>");
        }

        //两次密码一致 验证
        if($data['pwd'] != $data['repwd']){
            die("<script>alert('两次密码不一致');history.back(-1);</script>");
        }
        unset($data['repwd']);
        $data['pwd']    =   md5($data['pwd']);
        $data['add_time']    =  getTime();
        $data['login_time']    =  getTime();
        $res    =   M('users')->add($data);
        if($res) $this->success('注册成功,去登录吧','/Admin/Login/login');
        else die("<script>alert('注册失败');history.back(-1);</script>");

    }



    //退出
    public function quit()
    {
        session('user',null);
        $this->display('login');
    }

}

